home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-68k-src / machines / amiga68k / libsrc / prof / profiler.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  3KB  |  118 lines

  1. /* $VER: profiler.c V0.1 (28.08.98)
  2.  *
  3.  * Portable profiler for vbcc.
  4.  * Copyright (c) 1998  Frank Wille
  5.  *
  6.  * The vbcc profiler is split into a system independant (profiler)
  7.  * and in a system specific (prof_sysdep) part.
  8.  *
  9.  * v0.1  (28.08.98) phx
  10.  *       File created.
  11.  */
  12.  
  13. #include "profiler.h"
  14. #include "prof_sysdep.h"
  15.  
  16. static struct prof **hashtab = NULL;
  17.  
  18.  
  19. static void prof_output()
  20. {
  21.   if (hashtab) {
  22.     static char algnbuf[4] = { 0,0,0,0 };
  23.     FILE *fp;
  24.     struct prof *p;
  25.     int i,l;
  26.  
  27.     if (fp = fopen(PROFFILENAME,"w")) {
  28.       for (i=0; i<HASHTABSIZE; i++) {
  29.         p = hashtab[i];
  30.         while (p) {
  31.           l = strlen(p->name) + 1;
  32.           fwrite(p->name,1,l,fp);
  33.           if (l &= 3)  /* 32-bit align */
  34.             fwrite(algnbuf,1,4-l,fp);
  35.           fwrite(&p->called,1,sizeof(unsigned long),fp);
  36.           fwrite(&p->totaltime,1,sizeof(unsigned long),fp);
  37.           p = p->hashchain;
  38.         }
  39.       }
  40.       fclose(fp);
  41.     }
  42.     _prof_timerexit();
  43.     free(hashtab);
  44.   }
  45. }
  46.  
  47.  
  48. void _startprof(char *fname)
  49. {
  50.   __save_volatiles();
  51.  
  52.   if (!hashtab) {
  53.     /* _startprof is called first time - do initialization first */
  54.     if (!(hashtab = calloc(HASHTABSIZE,sizeof(struct prof *))))
  55.       goto xit;
  56.     if (!_prof_inittimer()) {
  57.       free(hashtab);
  58.       hashtab = NULL;
  59.       goto xit;
  60.     }
  61.     atexit(prof_output);
  62.   }
  63.  
  64.   {
  65.     /* search old or allocate new prof node */
  66.     unsigned long idx = ((unsigned long)fname>>2) & (HASHTABSIZE-1);
  67.     struct prof *last = NULL, *p = hashtab[idx];
  68.  
  69.     while (p) {
  70.       if (!strcmp(fname,p->name))
  71.         break;
  72.       last = p;
  73.       p = p->hashchain;
  74.     }
  75.     if (!p) {
  76.       /* allocate new prof node */
  77.       if (!(p = calloc(1,sizeof(struct prof))))
  78.         goto xit;
  79.       if (last)
  80.         last->hashchain = p;
  81.       else
  82.         hashtab[idx] = p;
  83.       memset(p,0,sizeof(struct prof));
  84.       p->name = fname;
  85.     }
  86.  
  87.     p->called++;
  88.     if (p->recursion_cnt++ == 0)
  89.       p->entrytime = _prof_time();
  90.   }
  91.  
  92. xit:
  93.   __restore_volatiles();
  94. }
  95.  
  96.  
  97. void _endprof(char *fname)
  98. {
  99.   __save_volatiles();
  100.  
  101.   if (hashtab) {
  102.     struct prof *p = hashtab[((unsigned long)fname>>2) & (HASHTABSIZE-1)];
  103.  
  104.     while (p) {
  105.       if (!strcmp(fname,p->name))
  106.         break;
  107.       p = p->hashchain;
  108.     }
  109.     if (!p)  /* this should not happen - maybe something trashed memory? */
  110.       fprintf(stderr,"_endprof: profiling data for %s have disappeared!\n",
  111.               fname);
  112.     if (--p->recursion_cnt == 0)
  113.       p->totaltime += _prof_time() - p->entrytime;
  114.   }
  115.  
  116.   __restore_volatiles();
  117. }
  118.